package com.zuoshenge.util;


import com.jcraft.jsch.*;
import com.zuoshenge.bean.JsonResult;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;

/**
 * 已弃用
 */
@Component
public class UploadFileToRemoteUtils {

    public static final Integer PORT = 22;
    public static final String HOST = "117.72.40.8";
    public static final String USERNAME = "zuoshenge";
    public static final String PASSWORD = "2002916Cjr,";
    public static final String FILE_DIR = "/opt/docker/apache/www/images/gxnu_course_sys";

    public static JsonResult uploadFileToRemoteServer(MultipartFile file) {
        String remoteFileName = file.getOriginalFilename(); // 远程服务器上的文件名

        JSch jsch = new JSch();
        Session session = null;
        ChannelSftp channel = null;
        try {
            session = jsch.getSession(USERNAME, HOST, PORT);
            session.setPassword(PASSWORD);

            // 禁用主机密钥检查（在生产环境中不推荐）
            java.util.Properties config = new java.util.Properties();
            config.put("StrictHostKeyChecking", "no");
            session.setConfig(config);

            session.connect();

            channel = (ChannelSftp) session.openChannel("sftp");
            channel.connect();

            // 将文件上传到远程服务器
            InputStream inputStream = file.getInputStream();
            channel.put(inputStream, FILE_DIR + "/" + remoteFileName);
            inputStream.close();

            // 获取文件访问URL（这里需要根据实际情况生成）
            String url = "http://117.72.40.8/images/gxnu_course_sys/" + remoteFileName;

            System.out.println("图片上传，访问URL：" + url);
            return JsonResult.builder().data(url).message("上传成功！").code("200").build();
        } catch (JSchException | SftpException e) {
            e.printStackTrace();
            return JsonResult.builder().data(null).message("SSH异常！").code("500").build();
        } catch (IOException e) {
            e.printStackTrace();
            return JsonResult.builder().data(null).message("IO异常！").code("500").build();
        } finally {
            if (channel != null) {
                channel.disconnect();
            }
            if (session != null) {
                session.disconnect();
            }
        }
    }

    public static void deleteFileToRemoteServer(String fileName) {
        JSch jsch = new JSch();
        Session session = null;
        ChannelExec channelExec = null;
        try {
            session = jsch.getSession(USERNAME, HOST, PORT); // 假设端口是22
            session.setPassword(PASSWORD);
            java.util.Properties config = new java.util.Properties();
            config.put("StrictHostKeyChecking", "no");
            session.setConfig(config);
            session.connect();
            String command = "rm -f " + FILE_DIR + "/" + fileName;
            channelExec = (ChannelExec) session.openChannel("exec");
            ((ChannelExec) channelExec).setCommand(command);
            channelExec.setInputStream(null);
            ((ChannelExec) channelExec).setErrStream(System.err);
            channelExec.connect();
        } catch (JSchException e) {
            e.printStackTrace();
        } finally {
            if (channelExec != null && channelExec.isConnected()) {
                channelExec.disconnect();
            }
            if (session != null && session.isConnected()) {
                session.disconnect();
            }
        }
    }
}
